package com.lcredu.seckill.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.lcredu.seckill.pojo.User;
import com.lcredu.seckill.service.UserService;
import com.lcredu.seckill.util.response.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 黎长容
 * @date 2025-08-29 15:07
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    //返回用户信息
//    @RequestMapping("/info")
//    @ResponseBody
//    public RespBean info(User user, String address) {
//        log.info("address->{}", address);
//        return RespBean.success(user);
//    }

    @RequestMapping("/info")
    @ResponseBody
    public RespBean info(User user) {
        log.info("当前用户id为{}", user.getId());
        return RespBean.success(user);
    }

    /**
     * 退出登录
     * 1. 根据传入的userId将redis里面的对象删除
     * 2. 删除本地cookie
     * 3. 返回登录页面
     */
    @RequestMapping("/loginOut")
    public String loginOut(Long userId, HttpServletRequest request, HttpServletResponse response) {
        userService.loginOutUser(userId, request, response);
        return "login";
    }


    //修改密码
    @RequestMapping("/updatePwd")
    @ResponseBody
    public RespBean updatePassword(@CookieValue(value = "userTicket") String ticket,
                                   @RequestParam("pwd") String password,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        return userService.updateUserPassword(ticket, password, request, response);
    }


}
